<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../rest.css">
    <style>
        .menu {
            width: 200px;
            padding: 5px;
            font-size: 12px;
            line-height: 30px;
            text-indent: 5px;
            border: 1px solid #dadce0;
            position: fixed;
            display: none;
        }

        .menu .list li:not(:last-child) {
            border-bottom: 1px solid #dadce0;
        }
    </style>
</head>

<body>
    <div class="menu">
        <ul class="list">
            <li class="copy">复制</li>
            <li class="paste">粘贴</li>
            <li class="cut">剪切</li>
            <li class="refresh">刷新</li>
        </ul>
    </div>
</body>
<script>
    var menu = document.getElementsByClassName("menu")[0];

    document.addEventListener("contextmenu", function (e) {
        var e = e || window.event;
        console.log(e);
        // 1. 先阻止默认菜单
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
        // 2. 显示我的自定义菜单    
        var x = e.clientX;
        var y = e.clientY;
        show(x, y);
        // menu.style.top = y + "px";
        // menu.style.display = "block";
    })

    document.addEventListener("mousedown", function () {
        menu.style.display = "none";
    })
    menu.addEventListener("mousedown", function (e) {
        var e = e || window.event;
        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    })
    menu.addEventListener("click", function (e) {
        var e = e || window.event;
        var target = e.target;
        if (target.className == "copy") {
            console.log("点击了复制");
            hide();
        } else if (target.className == "paste") {
            console.log("点击了粘贴");
            hide();
        } else if (target.className == "cut") {
            console.log("点击了剪切");
            hide();
        } else if (target.className == "refresh") {
            console.log("点击了刷新");
            hide();
            location.reload();
        }
    })

    function show(x, y) {
        menu.style.left = x + "px";
        menu.style.top = y + "px";
        menu.style.display = "block";
    }

    function hide() {
        menu.style.display = "none";

    }





</script>

</html>